﻿using PDFTool.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PDFTool.Controls
{
    /// <summary>
    /// UCPdf2Images.xaml 的交互逻辑
    /// </summary>
    public partial class UCPdf2Images : UserControl
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UCPdf2Images));
        public UCPdf2Images()
        {
            InitializeComponent();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {

        }
        private List<MyFileInfo> pdfFiles = null;
        private void btnSel_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog() { Filter = "PDF|*.pdf", Multiselect = true };
            if (fileDialog.ShowDialog() == true)
            {
                pdfFiles = new List<MyFileInfo>();
                for (int i = 0; i < fileDialog.FileNames.Length; i++)
                {
                    MyFileInfo info = new MyFileInfo() { Name = fileDialog.FileNames[i] };
                    FileInfo fi = new FileInfo(info.Name);
                    info.Size = fi.Length > 1024 * 1024 ? (fi.Length / (1024 * 1024.0)).ToString("f2") + "M" : (fi.Length / 1024).ToString() + "k";
                    pdfFiles.Add(info);
                }
                lvPdfs.ItemsSource = pdfFiles;
            }
        }

        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            if (working)
                return;
            if (pdfFiles == null || pdfFiles.Count == 0)
                return;
            string outpath = txtOutPath.Text.Trim();
            Task.Factory.StartNew(new Action(() =>
            {
                Run(pdfFiles, outpath);
            }));
        }
        bool working = false;
        private void Run(List<MyFileInfo> pdfFiles, string outPath)
        {
            if (working)
                return;
            working = true;
            try
            {
                int max = pdfFiles.Count;
                int i = 0;
                foreach (var item in pdfFiles)
                {
                    i++;
                    Common.PdfReader.ReadPdf2Image(item.Name, outPath);
                    UpdateProcess(i, max);
                }
            }
            catch (Exception ex)
            {
                logger.Error("处理失败", ex);
            }
            finally
            {
                working = false;
            }
        }
        private void UpdateProcess(int value, int max = 100)
        {
            if (this.progessBar != null && progessBar.IsInitialized)
            {
                this.progessBar.Dispatcher.BeginInvoke(new Action(() =>
                {
                    progessBar.Maximum = max;
                    progessBar.Value = value;
                }));
            }
        }
    }
}
